<?php

namespace app\admin\model\tree;

use think\Model;
use traits\model\SoftDelete;

class First extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'tree_first';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    /**
     * 获取栏目的所有子节点ID，无缓存
     * @param int  $id       栏目ID
     * @param bool $withself 是否包含自身
     * @return array
     */
    public static function getChildrenIds($id, $withself = false)
    {
        static $tree;
        if (!$tree) {
            $tree = \fast\Tree::instance();
            $tree->init(collection(First::order('weigh desc,id desc')->field('id,parent_id,name,type,status')->select())->toArray(), 'parent_id');
        }
        $childIds = $tree->getChildrenIds($id, $withself);
        return $childIds;
    }

    /**
     * 获取栏目的所有父节点ID，无缓存
     * @param int  $id       栏目ID
     * @param bool $withself 是否包含自身
     * @return array
     */
    public static function getParentsIds($id, $withself = false)
    {
        static $tree;
        if (!$tree) {
            $tree = \fast\Tree::instance();
            $tree->init(collection(First::order('weigh desc,id desc')->field('id,parent_id,name,type,status')->select())->toArray(), 'parent_id');
        }
        $childIds = $tree->getParentsIds($id, $withself);
        return $childIds;
    }

    







}
